package com.h2.gwt.util.ui;

import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class FloatingPanel extends PopupPanel {

   public FloatingPanel() {
      //nothing
   }
   
   public FloatingPanel(boolean autoHide) {
      super(autoHide);
   }
   
   public void moveToTopRight(final Widget widget) {
      boolean initiallyShowing = isShowing();
      boolean initiallyAnimated = isAnimationEnabled();

      if (!initiallyShowing) {
        setVisible(false);
        setAnimationEnabled(false);
        show();
      }
      
      widget.addAttachHandler(new Handler() {
         
         @Override
         public void onAttachOrDetach(AttachEvent event) {
//            int left = (widget.getOffsetWidth() - getOffsetWidth()) >> 1;
            int right = widget.getOffsetWidth();
            int top = widget.getOffsetHeight() - getOffsetHeight();
//            System.out.println("Right = " + right);
//            System.out.println("Top = " + top);
//            System.out.println("Left = " + left);
            
            setPopupPosition(
                  Math.max(right, 0),
                  Math.max(top, 0));
         }
      });

//      int left = (widget.getOffsetWidth() - getOffsetWidth()) >> 1;
//      int right = widget.getOffsetWidth();
//      int top = widget.getOffsetHeight() - getOffsetHeight();
//      System.out.println("Right = " + right);
//      System.out.println("Top = " + top);
//      System.out.println("Left = " + left);
//      
//      setPopupPosition(
//            Math.max(right, 0),
//            Math.max(top, 0));
//      setPopupPosition(Math.max(Window.getScrollLeft() + left, 0),
//            Math.max(Window.getScrollTop() + top, 0));

      if (!initiallyShowing) {
        setAnimationEnabled(initiallyAnimated);
         /*
          * Run the animation. The popup is already visible, so we can skip the
          * call to setState.
          */
        setVisible(true);
      }
   }
   
   public void moveTo(int left, int top) {
      boolean initiallyShowing = isShowing();
      boolean initiallyAnimated = isAnimationEnabled();

      if (!initiallyShowing) {
        setVisible(false);
        setAnimationEnabled(false);
        show();
      }

      setPopupPosition(left, top);

      if (!initiallyShowing) {
        setAnimationEnabled(initiallyAnimated);
         /*
          * Run the animation. The popup is already visible, so we can skip the
          * call to setState.
          */
        setVisible(true);
      }
   }
   
   public void moveToTopLeft() {
      boolean initiallyShowing = isShowing();
      boolean initiallyAnimated = isAnimationEnabled();

      if (!initiallyShowing) {
        setVisible(false);
        setAnimationEnabled(false);
        show();
      }

      int left = 200;
      int top = 200;
      setPopupPosition(left, top);

      if (!initiallyShowing) {
        setAnimationEnabled(initiallyAnimated);
         /*
          * Run the animation. The popup is already visible, so we can skip the
          * call to setState.
          */
        setVisible(true);
      }
   }
   
   public void moveToBottom() {
      boolean initiallyShowing = isShowing();
      boolean initiallyAnimated = isAnimationEnabled();

      if (!initiallyShowing) {
        setVisible(false);
        setAnimationEnabled(false);
        show();
      }

      int left = (Window.getClientWidth() - getOffsetWidth()) >> 1;
      int top = Window.getClientHeight() - getOffsetHeight();
      setPopupPosition(Math.max(Window.getScrollLeft() + left, 0),
            Math.max(Window.getScrollTop() + top, 0));

      if (!initiallyShowing) {
        setAnimationEnabled(initiallyAnimated);
         /*
          * Run the animation. The popup is already visible, so we can skip the
          * call to setState.
          */
        setVisible(true);
      }
   }

}
